package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.bo.PStoreReturnBo;
import com.ruoyi.system.domain.vo.PStoreReturnVo;
import com.ruoyi.system.domain.PStoreReturn;
import com.ruoyi.system.mapper.PStoreReturnMapper;
import com.ruoyi.system.service.IPStoreReturnService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 商城订单退款Service业务层处理
 *
 * @author ruoyi
 * @date 2022-08-05
 */
@RequiredArgsConstructor
@Service
public class PStoreReturnServiceImpl implements IPStoreReturnService {

    private final PStoreReturnMapper baseMapper;

    /**
     * 查询商城订单退款
     */
    @Override
    public PStoreReturnVo queryById(Long id){
        return baseMapper.selectLeftVoById(id);
    }

    /**
     * 查询商城订单退款列表
     */
    @Override
    public TableDataInfo<PStoreReturnVo> queryPageList(PStoreReturnBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<PStoreReturn> lqw = buildQueryWrapper(bo);
        Page<PStoreReturnVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询商城订单退款列表
     */
    @Override
    public List<PStoreReturnVo> queryList(PStoreReturnBo bo) {
        LambdaQueryWrapper<PStoreReturn> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PStoreReturn> buildQueryWrapper(PStoreReturnBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PStoreReturn> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getProductId() != null, PStoreReturn::getProductId, bo.getProductId());
        lqw.eq(bo.getProductCount() != null, PStoreReturn::getProductCount, bo.getProductCount());
        lqw.eq(StringUtils.isNotBlank(bo.getProductType()), PStoreReturn::getProductType, bo.getProductType());
        lqw.eq(StringUtils.isNotBlank(bo.getInfo()), PStoreReturn::getInfo, bo.getInfo());
        lqw.eq(bo.getPrice() != null, PStoreReturn::getPrice, bo.getPrice());
        lqw.eq(StringUtils.isNotBlank(bo.getDescInfo()), PStoreReturn::getDescInfo, bo.getDescInfo());
        lqw.eq(StringUtils.isNotBlank(bo.getUrl()), PStoreReturn::getUrl, bo.getUrl());
        lqw.eq(StringUtils.isNotBlank(bo.getCheckType()), PStoreReturn::getCheckType, bo.getCheckType());
        return lqw;
    }

    /**
     * 新增商城订单退款
     */
    @Override
    public Boolean insertByBo(PStoreReturnBo bo) {
        PStoreReturn add = BeanUtil.toBean(bo, PStoreReturn.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改商城订单退款
     */
    @Override
    public Boolean updateByBo(PStoreReturnBo bo) {
        PStoreReturn update = BeanUtil.toBean(bo, PStoreReturn.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(PStoreReturn entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除商城订单退款
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public TableDataInfo<PStoreReturnVo> queryMyPageList(PStoreReturnBo bo, PageQuery pageQuery) {
        QueryWrapper<PStoreReturnVo> lqw = Wrappers.query();
        lqw.like(StringUtils.isNotBlank(bo.getUsername()), "u.user_name", bo.getUsername());
        lqw.like(StringUtils.isNotBlank(bo.getStoreName()), "p.store_name", bo.getStoreName());
        lqw.eq(StringUtils.isNotBlank(bo.getProductType()), "r.product_type", bo.getProductType());
        lqw.eq(StringUtils.isNotBlank(bo.getCheckType()), "r.check_type", bo.getCheckType());
        Page<PStoreReturnVo> result = baseMapper.selectMyVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }
}
